package com.hua.api;

import com.hua.api.core.framework.annotation.AnonymousAccess;
import com.hua.api.core.framework.util.Result;
import com.hua.api.core.framework.util.exception.ApiError;
import com.hua.api.core.util.SpringContextHolder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.ApplicationPidFileWriter;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


// https://www.cnblogs.com/yufeng218/p/11509486.html

@EnableAsync
@RestController
@SpringBootApplication
@EnableTransactionManagement
@ServletComponentScan
@EnableJms
public class OfficialApplication {

    public static void main(String[] args) {
        // SpringApplication.run(OfficialApplication.class, args);
        SpringApplication springApplication = new SpringApplication(OfficialApplication.class);

        // 监控应用的PID，启动时可指定PID路径：--spring.pid.file=/home/eladmin/app.pid
        // 或者在 application.yml 添加文件路径，方便 kill，kill `cat /home/eladmin/app.pid`
        springApplication.addListeners(new ApplicationPidFileWriter());
        springApplication.run(args);
    }

    @Bean
    public SpringContextHolder springContextHolder(){
        return new SpringContextHolder();
    }

    @Bean
    public ServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory fa = new TomcatServletWebServerFactory();
        fa.addConnectorCustomizers(connector -> connector.setProperty("relaxedQueryChars", "[]{}"));
        return fa;
    }

    /**
     * 访问首页提示
     */
    @AnonymousAccess
    @GetMapping("/")
    public String index() {
        return "hua-api-master started successfully";
    }


    /**
     * /error
     */
    @AnonymousAccess
    @GetMapping("/error")
    public Result error() {
        return Result.error(ApiError.Type.LOGIC_ERROR.toException());
    }


}
